﻿/* **************************************************************************
 *
 * Copyright 2007-2010 Jeff Hardy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * *************************************************************************/

using System;
using System.Collections;
using Microsoft.Web.Management.Client;
using Microsoft.Web.Management.Client.Win32;
using Microsoft.Web.Management.Server;

namespace NWSGI.ManagementClient
{
    class NwsgiPage: ModulePropertiesPage
    {
        private PropertyBag bag;
        private PropertyBag clone;

        private NwsgiModuleServiceProxy serviceProxy;
        private NwsgiModuleServiceProxy Proxy
        {
            get
            {
                if(this.serviceProxy == null)
                {
                    this.serviceProxy = (NwsgiModuleServiceProxy)base.CreateProxy(typeof(NwsgiModuleServiceProxy));
                }
                return this.serviceProxy;
            }
        }

        protected override bool CanApplyChanges { get { return this.HasChanges; } }
        protected override bool ConfigNamesPresent { get { return true; } }

        private NwsgiPage.PageTaskList taskList;
        protected override TaskListCollection Tasks
        {
            get
            {
                if(this.taskList == null)
                {
                    this.taskList = new NwsgiPage.PageTaskList(this);
                }

                TaskListCollection tasks = base.Tasks;
                tasks.Add(this.taskList);
                return tasks;
            }
        }

        protected override PropertyBag GetProperties()
        {
            return this.Proxy.GetConfiguration();
        }

        protected override void ProcessProperties(PropertyBag properties)
        {
            this.bag = properties;

            this.clone = this.bag.Clone(this.ReadOnly);
            WsgiInfo info = base.TargetObject as WsgiInfo;
            if(info == null)
            {
                info = new WsgiInfo(this, this.clone);
                base.TargetObject = info;
            }
            else
            {
                info.Initialize(this.clone);
            }

            base.ClearDirty();
        }

        protected override PropertyBag UpdateProperties(out bool updateSuccessful)
        {
            updateSuccessful = false;
            try
            {
                this.Proxy.UpdateConfiguration(this.clone);
                this.bag = this.clone;
                updateSuccessful = true;
            }
            catch(Exception e)
            {
                base.DisplayErrorMessage(e, Resources.ResourceManager);
            }
            return this.bag;
        }

        private sealed class PageTaskList : TaskList
        {
            private NwsgiPage owner;

            public PageTaskList(NwsgiPage owner)
            {
                this.owner = owner;
            }

            public override ICollection GetTaskItems()
            {
                ArrayList list = new ArrayList();

                list.Add(new MethodTaskItem("OpenPythonPaths", "Edit Python Paths...", "Actions"));
                list.Add(new MethodTaskItem("OpenWsgiEnviron", "Edit WSGI Environment...", "Actions"));
                list.Add(new MethodTaskItem("OpenScriptMappings", "Edit Script Mappings...", "Actions"));

                return list;
            }

            public void OpenPythonPaths()
            {
                this.owner.Navigate(typeof(PythonPathsPage));
            }

            public void OpenWsgiEnviron()
            {
                this.owner.Navigate(typeof(WsgiEnvironPage));
            }

            public void OpenScriptMappings()
            {
                this.owner.Navigate(typeof(ScriptMappingsPage));
            }
        }
    }
}
